
import json
from app.utils.Aliyun.AliyunException import AliyunException
from app.utils.Aliyun.AliyunBase import AliyunBase
from alibabacloud_tea_openapi import models
from alibabacloud_dysmsapi20170525 import models as smsModels
from alibabacloud_dysmsapi20170525.client import Client

class AliyunSmsUtils(AliyunBase):
    smsSignName=""
    def getClient(self)->Client:
        config= models.Config(access_key_id=self.accessKey,
                             access_key_secret=self.accessSecret,
                             endpoint=self.getEndpoint())
        return Client(config)
    def getEndpoint(self):
        return "dysmsapi.aliyuncs.com"
    
    def sendMessage(self,phone,templateCode,templateParam):
        request=smsModels.SendSmsRequest()
        request.phone_numbers=phone
        request.sign_name=self.smsSignName
        request.template_code=templateCode
        request.template_param=json.dumps(templateParam)
        response=self.getClient().send_sms(request)
        if response.body.code!="OK":
            raise AliyunException(f"短信发送失败：{response.body.code} {response.body.message}")
        return response.body